﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using cMES1000.Services;
using Newtonsoft.Json;
using xClient.Framework;
using xClient.Framework.StaticResource;

namespace cMES1000
{
    public partial class Edit : Form
    {
        public Edit(string command, params object[] values)
        {
            InitializeComponent();

            if (command.Equals("ADD"))
            {
                this.Text = "新增供应商";
                this.txtVendorCode.Text = "";
                this.txtVendorName.Text = "";
                this.txtVendorDesc.Text = "";
                this.txtVendorAddr.Text = "";
                this.txtVendorPhone.Text = "";
                this.txtVendorContact.Text = "";
            }
            if (command.Equals("EDIT"))
            {
                this.Text = "修改供应商";
                this.txtVendorCode.Enabled = false;
                this.txtVendorCode.Text = values[0].ToString();
                this.txtVendorName.Text = values[1].ToString();
                this.txtVendorDesc.Text = values[2].ToString();
                this.txtVendorAddr.Text = values[3].ToString();
                this.txtVendorPhone.Text = values[4].ToString();
                this.txtVendorContact.Text = values[5].ToString();
            }

            btnEdit.Click += delegate
            {
                if (command.Equals("ADD"))
                {
                    if (txtVendorCode.Text.Length < 1) { return; }
                    if (txtVendorName.Text.Length < 1) { return; }
                    if (txtVendorDesc.Text.Length < 1) { return; }
                    if (txtVendorAddr.Text.Length < 1) { return; }
                    if (txtVendorPhone.Text.Length < 1) { return; }
                    if (txtVendorContact.Text.Length < 1) { return; }

                    string vm = JsonConvert.SerializeObject(new
                    {
                        vcode = txtVendorCode.Text,
                        vname = txtVendorName.Text,
                        vdesc = txtVendorDesc.Text,
                        vaddr = txtVendorAddr.Text,
                        vphone = txtVendorPhone.Text,
                        vcontact = txtVendorContact.Text,
                        cby = StaticDictionary.Dictionary.UserID,
                        mby = StaticDictionary.Dictionary.UserID
                    });

                    ExecutionResult result = ServiceMethod.AddVendor(vm);

                    if (result.Status)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.No;
                        MessageBox.Show(result.Message, "系统消息");
                    }
                }

                if (command.Equals("EDIT"))
                {
                    if (txtVendorCode.Text.Length < 1) { return; }
                    if (txtVendorName.Text.Length < 1) { return; }
                    if (txtVendorDesc.Text.Length < 1) { return; }
                    if (txtVendorAddr.Text.Length < 1) { return; }
                    if (txtVendorPhone.Text.Length < 1) { return; }
                    if (txtVendorContact.Text.Length < 1) { return; }

                    string vm = JsonConvert.SerializeObject(new
                    {
                        vcode = txtVendorCode.Text,
                        vname = txtVendorName.Text,
                        vdesc = txtVendorDesc.Text,
                        vaddr = txtVendorAddr.Text,
                        vphone = txtVendorPhone.Text,
                        vcontact = txtVendorContact.Text,
                        cby = StaticDictionary.Dictionary.UserID,
                        mby = StaticDictionary.Dictionary.UserID
                    });

                    ExecutionResult result = ServiceMethod.UpdateVendor(vm);

                    if (result.Status)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.No;
                        MessageBox.Show(result.Message, "系统消息");
                    }
                }
            };
        }
    }
}
